package com.ruoyi.web.controller.stores;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.stores.MaintenanceList;
import com.ruoyi.system.service.IMaintenanceListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/2/20 16:54
 */
@Api(tags = "贝序设备维护列表")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Slf4j
@RequestMapping("/maintenance/man" )
public class StoresMaintenanceListController extends BaseController {


    @Autowired
    IMaintenanceListService iMaintenanceListService;


    /**
     * 查询设备维护分页列表
     * @param plan
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('stores:man:list')")
    @ApiOperation(value = "查询设备维护列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody MaintenanceList plan){
        startPage();
        LambdaQueryWrapper<MaintenanceList> lqw = new LambdaQueryWrapper<MaintenanceList>();
        if (StringUtils.isNotBlank(plan.getDeviceName())){
            lqw.eq(MaintenanceList::getDeviceName,plan.getDeviceName());
        }
        if (StringUtils.isNotBlank(plan.getEquipmentManufacturer())){
            lqw.like(MaintenanceList::getEquipmentManufacturer ,plan.getEquipmentManufacturer());
        }
        if (plan.getMaintainDate()!=null){
            lqw.eq(MaintenanceList::getMaintainDate ,plan.getMaintainDate());
        }
        if (plan.getPlannedMaintenanceDate()!=null){
            lqw.eq(MaintenanceList::getPlannedMaintenanceDate ,plan.getPlannedMaintenanceDate());
        }
        Date date = new Date();
        if(date.equals(plan.getPlannedMaintenanceDate())){
            throw new CustomException("设备需要维护，请及时处理！");
        }
        List<MaintenanceList> list = iMaintenanceListService.list(lqw);
        return getDataTable(list);
    }


    /**
     * 新增设备维护
     */
    //@PreAuthorize("@ss.hasPermi('stores:man:add')")
    @Log(title = "新增设备维护", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增设备维护")
    @PostMapping("/add")
    public AjaxResult add(@ApiParam(value = "设备维护实体类", required = true) @RequestBody MaintenanceList ordersCustomer)
    {

        return toAjax(iMaintenanceListService.insertMaintenanceList(ordersCustomer));
    }

    /**
     * 修改设备维护
     */
    //@PreAuthorize("@ss.hasPermi('orders:man:update')")
    @Log(title = "设备维护", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改设备维护")
    @PutMapping("/update")
    public AjaxResult update(@ApiParam(value = "设备维护实体类", required = true) @RequestBody MaintenanceList ordersCustomer)
    {

        return toAjax(iMaintenanceListService.updateMaintenanceList(ordersCustomer));
    }



    /**
     * 删除设备维护
     */
    //@PreAuthorize("@ss.hasPermi('stores:man:id')")
    @DeleteMapping("delete/id")
    @ApiOperation(value = "删除设备维护")
    public AjaxResult DeleteById(Long id) {
        iMaintenanceListService.deleteRoleById(id);
        return AjaxResult.success("删除成功");
    }


}
